package com.chatdating.service.impl;

import com.chatdating.entity.Tag;
import com.chatdating.entity.dto.MatchDto;
import com.chatdating.mapper.LikeTagMapper;
import com.chatdating.mapper.LikeUserMapper;
import com.chatdating.service.LikeUserService;
import com.chatdating.service.TagService;
import com.chatdating.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LikeUserServiceImpl implements LikeUserService {

    @Autowired
    private LikeUserMapper likeUserMapper;

    @Autowired
    private LikeTagMapper likeTagMapper;

    @Autowired
    private TagService tagService;

    @Override
    public void addLikeUser(Integer userId, Integer likeUserId) {
        likeUserMapper.addOne(userId,likeUserId);
        //这里还要把喜欢人的标签收集起来并计数，推荐使用
        List<Tag> tags = tagService.findAll(likeUserId);
        for (Tag tag : tags) {
            if(EmptyUtil.isEmpty(likeTagMapper.findOne(userId,tag.getId()))) {
                likeTagMapper.insertLikeTag(userId,tag.getId(),1);
            }else{
                int num = likeTagMapper.findOne(userId,tag.getId()).getNum()+1;
                likeTagMapper.updateLikeTag(userId,tag.getId(),num);
            }
        }
    }

    @Override
    public void deleteLikeUser(Integer userId, Integer noLikeUserId) {
        likeUserMapper.deleteOne(userId,noLikeUserId);
    }

    @Override
    public List<MatchDto> findAllLikeUser(Integer userId) {
        List<MatchDto> matchDtoList = likeUserMapper.findAll(userId);
        for (MatchDto matchDto : matchDtoList) {
            int id = matchDto.getId();
            List<Tag> tags = tagService.findAll(id);
            matchDto.setTags(tags);
        }
        return matchDtoList;
    }
}
